import datetime
from django.db import models
from django.utils import timezone


# Create your models here.
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def __unicode__(self):
        return self.question_text

    def was_published_recently(self):  # 通过函数定义虚拟字段
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'  # 指定排序字段
    was_published_recently.boolean = True  # 如果指定了这个字段显示的时候体现为图表，比较美观
    was_published_recently.short_description = 'Published recently?'  # 重定义字段显示名称,但是非数字段似乎不生效


class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

    def __unicode__(self):
        return self.choice_text
